#include "tcpwriteinstructionview.h"
#include "ui_tcpwriteinstructionview.h"
#include "../../programView/programview.h"

TCPWriteInstructionView::TCPWriteInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TCPWriteInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    tcpWriteClientIPVec.resize(4);
    tcpWriteClientIPVec[0] = 127;
    tcpWriteClientIPVec[1] = 0;
    tcpWriteClientIPVec[2] = 0;
    tcpWriteClientIPVec[3] = 1;

    tcpWriteServerIPVec.resize(4);
    tcpWriteServerIPVec[0] = 192;
    tcpWriteServerIPVec[1] = 168;
    tcpWriteServerIPVec[2] = 111;
    tcpWriteServerIPVec[3] = 36;

    ip1 = 0;
    ip2 = 0;
    ip3 = 0;
    ip4 = 0;

    ui->RegLength_Edit->setValidator(new QIntValidator(0,10000,this));
    ui->PORT_Edit->setValidator(new QIntValidator(0,65536,this));

    ui->lineEdit_ip1->setValidator(new QIntValidator(0,255,this));
    ui->lineEdit_ip2->setValidator(new QIntValidator(0,255,this));
    ui->lineEdit_ip3->setValidator(new QIntValidator(0,255,this));
    ui->lineEdit_ip4->setValidator(new QIntValidator(0,255,this));

}

void TCPWriteInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox_2->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void TCPWriteInstructionView::getInput()
{
    if(tcpWriteType == E_TCP_WRITE_TYPE_CONST)
    {
        tcpWriteConstValue = ui->Const_Edit->text();
        tcpWriteRegBegAddress = 0;
        regLengthValue = 0;
    }
    else if(tcpWriteType == E_TCP_WRITE_TYPE_REGISTER)
    {
        tcpWriteConstValue = '0';
        tcpWriteRegBegAddress = ui->RegBeginAddress_Edit->text().toInt();
        regLengthValue = ui->RegLength_Edit->text().toInt();
    }

    if(tcpWriteBusType == E_TCP_WRITE_BUS_CLIENT)
    {
        tcpWriteServerIPVec[0] = ui->lineEdit_ip1->text().toInt();
        tcpWriteServerIPVec[1] = ui->lineEdit_ip2->text().toInt();
        tcpWriteServerIPVec[2] = ui->lineEdit_ip3->text().toInt();
        tcpWriteServerIPVec[3] = ui->lineEdit_ip4->text().toInt();
    }
    else if(tcpWriteBusType == E_TCP_WRITE_BUS_SERVER)
    {
        tcpWriteClientIPVec[0] = ui->lineEdit_ip1->text().toInt();
        tcpWriteClientIPVec[1] = ui->lineEdit_ip2->text().toInt();
        tcpWriteClientIPVec[2] = ui->lineEdit_ip3->text().toInt();
        tcpWriteClientIPVec[3] = ui->lineEdit_ip4->text().toInt();
    }

//    qDebug() << "tcpWriteIPVec[0] = " << ui->lineEdit_ip1->text().toInt();;
//    qDebug() << "tcpWriteIPVec[1] = " << ui->lineEdit_ip2->text().toInt();
//    qDebug() << "tcpWriteIPVec[2] = " << ui->lineEdit_ip3->text().toInt();
//    qDebug() << "tcpWriteIPVec[3] = " << ui->lineEdit_ip4->text().toInt();

    tcpWritePort = ui->PORT_Edit->text().toInt();
    jmpLBValue = ui->JMP_LB_SpinBox->text().toInt();
    noteStr = ui->note_lineEdit->text();

}

void TCPWriteInstructionView::updateView()
{
    ui->WriteType_Const_radioButton->setChecked(false);
    ui->WriteType_Reg_radioButton->setChecked(false);

    ui->CLIENT_radioButton->setChecked(false);
    ui->SERVER_radioButton->setChecked(false);

    ui->Const_Edit->setEnabled(false);
    ui->RegBeginAddress_Edit->setEnabled(false);
    ui->RegLength_Edit->setEnabled(false);

    if(tcpWriteType == E_TCP_WRITE_TYPE_CONST)
    {
        ui->WriteType_Const_radioButton->setChecked(true);
        ui->Const_Edit->setEnabled(true);
        ui->Const_Edit->setText(tcpWriteConstValue);
    }
    else if(tcpWriteType == E_TCP_WRITE_TYPE_REGISTER)
    {
        ui->WriteType_Reg_radioButton->setChecked(true);
        ui->RegBeginAddress_Edit->setEnabled(true);
        ui->RegLength_Edit->setEnabled(true);
        ui->RegBeginAddress_Edit->setText(QString::number(tcpWriteRegBegAddress));
        ui->RegLength_Edit->setText(QString::number(regLengthValue));
    }

    if(tcpWriteBusType == E_TCP_WRITE_BUS_CLIENT)
    {
        ui->CLIENT_radioButton->setChecked(true);
        ui->IP_Label->setText("Server IP:");
        ui->lineEdit_ip1->setText(QString::number(tcpWriteServerIPVec[0]));
        ui->lineEdit_ip2->setText(QString::number(tcpWriteServerIPVec[1]));
        ui->lineEdit_ip3->setText(QString::number(tcpWriteServerIPVec[2]));
        ui->lineEdit_ip4->setText(QString::number(tcpWriteServerIPVec[3]));

    }
    else if(tcpWriteBusType == E_TCP_WRITE_BUS_SERVER)
    {
        ui->SERVER_radioButton->setChecked(true);
        ui->IP_Label->setText("Client IP:");
        ui->lineEdit_ip1->setText(QString::number(tcpWriteClientIPVec[0]));
        ui->lineEdit_ip2->setText(QString::number(tcpWriteClientIPVec[1]));
        ui->lineEdit_ip3->setText(QString::number(tcpWriteClientIPVec[2]));
        ui->lineEdit_ip4->setText(QString::number(tcpWriteClientIPVec[3]));

    }

//    qDebug() << "tcpWriteClientIPVec[0] = " << tcpWriteClientIPVec[0];
//    qDebug() << "tcpWriteClientIPVec[1] = " << tcpWriteClientIPVec[1];
//    qDebug() << "tcpWriteClientIPVec[2] = " << tcpWriteClientIPVec[2];
//    qDebug() << "tcpWriteClientIPVec[3] = " << tcpWriteClientIPVec[3];

    ui->PORT_Edit->setText(QString::number(tcpWritePort));

    ui->JMP_LB_SpinBox->setValue(jmpLBValue);
    ui->note_lineEdit->setText(noteStr);

}

void TCPWriteInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/TCPWriteInstructionViewEnglish.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

void TCPWriteInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNoteSignal();

}

void TCPWriteInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void TCPWriteInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionOut);
}

void TCPWriteInstructionView::on_WriteType_Reg_radioButton_clicked()
{
    tcpWriteType = E_TCP_WRITE_TYPE_REGISTER;
    ui->Const_Edit->setEnabled(false);
    ui->RegBeginAddress_Edit->setEnabled(true);
    ui->RegLength_Edit->setEnabled(true);
}

void TCPWriteInstructionView::on_WriteType_Const_radioButton_clicked()
{
    tcpWriteType = E_TCP_WRITE_TYPE_CONST;
    ui->Const_Edit->setEnabled(true);
    ui->RegBeginAddress_Edit->setEnabled(false);
    ui->RegLength_Edit->setEnabled(false);
}

void TCPWriteInstructionView::on_SERVER_radioButton_clicked()
{
    ip1 = ui->lineEdit_ip1->text().toInt();
    ip2 = ui->lineEdit_ip2->text().toInt();
    ip3 = ui->lineEdit_ip3->text().toInt();
    ip4 = ui->lineEdit_ip4->text().toInt();

    tcpWriteBusType = E_TCP_WRITE_BUS_SERVER;
    ui->IP_Label->setText("Client IP:");
    ui->lineEdit_ip1->setText("127");
    ui->lineEdit_ip2->setText("0");
    ui->lineEdit_ip3->setText("0");
    ui->lineEdit_ip4->setText("1");
}

void TCPWriteInstructionView::on_CLIENT_radioButton_clicked()
{
    tcpWriteBusType = E_TCP_WRITE_BUS_CLIENT;

    ui->lineEdit_ip1->setText(QString::number(ip1));
    ui->lineEdit_ip2->setText(QString::number(ip2));
    ui->lineEdit_ip3->setText(QString::number(ip3));
    ui->lineEdit_ip4->setText(QString::number(ip4));

    ui->IP_Label->setText("Server IP:");
}
